<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 100px;
        height: 100px;
        background-color: red;
        position: absolute;
        left: 0px;
        top: 0px;
      }
    </style>
  </head>

  <body>
    <div class="box"></div>
    <script>
      function move(boxEle, dir, target) {
        return new Promise((resolve) => {
          var timer = setInterval(function () {
            var num3 = parseInt(getComputedStyle(boxEle)[dir]);
            num3 += 5 * (target > num3 ? 1 : -1);
            boxEle.style[dir] = num3 + "px";
            if (num3 == target) {
              clearInterval(timer);
              console.log("停止了");
              resolve();
            }
          }, 10);
        });
      }
      var boxEle = document.querySelector(".box");
      (async function () {
        try {
          await move(boxEle, "left", 500);
          console.log(111);
          await move(boxEle, "top", 500);
          console.log(222);
          await move(boxEle, "left", 0);
          console.log(333);
          await move(boxEle, "top", 0);
          console.log(444);
        } catch (err) {
          console.log(err);
        }
      })();
    </script>
  </body>
</html>
